/**
 * 打开借款单选择器
 */
function openSelectborrows(type){
	var reimbTotal = document.getElementById("reimbTotal").value;
	var balance = document.getElementById("balance").value;
    if (Number(balance) == 0 ) {
       alert("余款金额为0,不允许选择借款单进行销账业务！");	
       return;
    }
    
	var totalBorrowIds = document.getElementById("totalBorrowIds").value;
	var partBorrowIds = document.getElementById("partBorrowIds").value;
	
	if (type == 'total'){
	  var url = URL_PREFIX + '/borrow/selectBorrowIndex.do?borrowIds=' + partBorrowIds + '&type=checkbox';
	} else {
	  var url = URL_PREFIX + '/borrow/selectBorrowIndex.do?borrowIds=' + totalBorrowIds + '&type=radio';
	}
	var styles = "dialogWidth:585px; dialogHeight:430px; status:no; scroll:no; help:no";
	var selectedBorrows = window.showModalDialog(url, null, styles);
	
    var totalAmount;
    var totalRepayAmount;
    var totalNoRepayAmount;
    var partAmount;
    var partRepayAmount;
    var partNoRepayAmount;
	if (!selectedBorrows){
		//如果没有选择用户不做任何操作
	} else {
		if (type == 'total'){
		  totalAmount = 0;
		  totalRepayAmount = 0;
		  totalNoRepayAmount = 0;	
		  $("#totalBorrowNos").val(selectedBorrows.nos);
		  $("#totalBorrowIds").val(selectedBorrows.ids); 
		  $("#totalBorrowAmounts").val(selectedBorrows.borrowAmounts); 
		  $("#totalRepayAmounts").val(selectedBorrows.rePayAmounts); 
		  var borrowAmountsStr = $("#totalBorrowAmounts").val();
	      var borrowAmounts = borrowAmountsStr.split(",");
	      for(var i = 0; i < borrowAmounts.length - 1; i++){
	    	  totalAmount += Number(borrowAmounts[i]);
	      }
		  
		  var rePayAmountsStr = $("#totalRepayAmounts").val();
		  var rePayAmounts = rePayAmountsStr.split(",");
	      for(var i = 0; i < rePayAmounts.length - 1; i++){
	    	  totalRepayAmount += Number(rePayAmounts[i]);
		  }
	     
	      totalNoRepayAmount = Number(totalAmount) - Number(totalRepayAmount);
	      $("#totalNoRepayAmounts").val(totalNoRepayAmount.toFixed(2));
	      
	      if (Number($("#totalNoRepayAmounts").val()) == 0) {
			  $("#totalBorrowNos").val("");
			  $("#totalBorrowIds").val(""); 
			  $("#totalBorrowAmounts").val(""); 
			  $("#totalRepayAmounts").val(""); 
			  $("#totalNoRepayAmounts").val("");
			  
			  $("#partBorrowNos").val("");
			  $("#partBorrowIds").val(""); 
			  $("#partBorrowAmounts").val(""); 
			  $("#partRepayAmounts").val(""); 
			  $("#partNoRepayAmounts").val("");
			  $("#borrowAmount").val("");
			  zjs(); 
			  return false; 
	      }
	      
			 if (Number($("#partNoRepayAmounts").val()) > 0) {
				 if (Number($("#totalNoRepayAmounts").val()) > 0 && reimbTotal - Number($("#partNoRepayAmounts").val()) < Number($("#totalNoRepayAmounts").val())) {
					 alert("报销金额小于销账金额,请您操作部分销账业务！");
					  $("#totalBorrowNos").val("");
					  $("#totalBorrowIds").val(""); 
					  $("#totalBorrowAmounts").val(""); 
					  $("#totalRepayAmounts").val(""); 
					  $("#totalNoRepayAmounts").val("");
					  
					  $("#partBorrowNos").val("");
					  $("#partBorrowIds").val(""); 
					  $("#partBorrowAmounts").val(""); 
					  $("#partRepayAmounts").val(""); 
					  $("#partNoRepayAmounts").val("");
					  $("#borrowAmount").val("");
					  zjs(); 
					  return false;
				 }
			 } else {
				 if (Number($("#totalNoRepayAmounts").val()) > 0 && reimbTotal < Number($("#totalNoRepayAmounts").val())) {
					 alert("报销金额小于销账金额,请您操作部分销账业务！");
					  $("#totalBorrowNos").val("");
					  $("#totalBorrowIds").val(""); 
					  $("#totalBorrowAmounts").val(""); 
					  $("#totalRepayAmounts").val(""); 
					  $("#totalNoRepayAmounts").val("");
					  
					  $("#partBorrowNos").val("");
					  $("#partBorrowIds").val(""); 
					  $("#partBorrowAmounts").val(""); 
					  $("#partRepayAmounts").val(""); 
					  $("#partNoRepayAmounts").val("");
					  $("#borrowAmount").val("");
					  zjs(); 
					  return false;
				 }
			 }
		} else {
		  partAmount = 0;
		  partRepayAmount = 0;
		  partNoRepayAmount = 0;			
		  $("#partBorrowNos").val(selectedBorrows.nos);
		  $("#partBorrowIds").val(selectedBorrows.ids);
		  $("#partBorrowAmounts").val(selectedBorrows.borrowAmounts); 
		  $("#partRepayAmounts").val(selectedBorrows.rePayAmounts); 
		  var borrowAmountsStr = $("#partBorrowAmounts").val();
	      var borrowAmounts = borrowAmountsStr.split(",");
	      for(var i = 0; i < borrowAmounts.length - 1; i++){
	    	  partAmount += Number(borrowAmounts[i]);
	      }
		  
		  var rePayAmountsStr = $("#partRepayAmounts").val();
		  var rePayAmounts = rePayAmountsStr.split(",");
	      for(var i = 0; i < rePayAmounts.length - 1; i++){
	    	  partRepayAmount += Number(rePayAmounts[i]);
		  }
	      partNoRepayAmount = partAmount - partRepayAmount;
	      $("#partNoRepayAmounts").val(partNoRepayAmount.toFixed(2));
	      
	      if (Number($("#partNoRepayAmounts").val()) == 0) {
			  $("#totalBorrowNos").val("");
			  $("#totalBorrowIds").val(""); 
			  $("#totalBorrowAmounts").val(""); 
			  $("#totalRepayAmounts").val(""); 
			  $("#totalNoRepayAmounts").val("");
			  
			  $("#partBorrowNos").val("");
			  $("#partBorrowIds").val(""); 
			  $("#partBorrowAmounts").val(""); 
			  $("#partRepayAmounts").val(""); 
			  $("#partNoRepayAmounts").val("");
			  $("#borrowAmount").val("");
			  zjs(); 
			  return false; 
	      }
	      
			 if (Number($("#totalNoRepayAmounts").val()) > 0) {
				 if (Number($("#partNoRepayAmounts").val()) > 0 && reimbTotal - Number($("#totalNoRepayAmounts").val()) >= Number($("#partNoRepayAmounts").val())) {
					  alert("报销金额已大于或等于部分销账的未销账金额,请您操作销账业务！");
					  $("#partBorrowNos").val("");
					  $("#partBorrowIds").val(""); 
					  $("#partBorrowAmounts").val(""); 
					  $("#partRepayAmounts").val(""); 
					  $("#partNoRepayAmounts").val("");
					  
					  $("#totalBorrowNos").val("");
					  $("#totalBorrowIds").val(""); 
					  $("#totalBorrowAmounts").val(""); 
					  $("#totalRepayAmounts").val(""); 
					  $("#totalNoRepayAmounts").val("");
					  $("#borrowAmount").val("");
					  zjs(); 
					  return false; 
				 }
			 } else {
				 if (Number($("#partNoRepayAmounts").val()) > 0 && reimbTotal >= Number($("#partNoRepayAmounts").val())) {
					  alert("报销金额已大于或等于部分销账的未销账金额,请您操作销账业务！");
					  $("#partBorrowNos").val("");
					  $("#partBorrowIds").val(""); 
					  $("#partBorrowAmounts").val(""); 
					  $("#partRepayAmounts").val(""); 
					  $("#partNoRepayAmounts").val("");
					  
					  $("#totalBorrowNos").val("");
					  $("#totalBorrowIds").val(""); 
					  $("#totalBorrowAmounts").val(""); 
					  $("#totalRepayAmounts").val(""); 
					  $("#totalNoRepayAmounts").val("");
					  $("#borrowAmount").val("");
					  zjs(); 
					  return false; 
				 }
			 }	
		}

		 $("#offsetAmount").val(toFix(reimbTotal - Number($("#totalNoRepayAmounts").val())));		 
         var borrowTotal = Number($("#totalNoRepayAmounts").val()) + Number($("#partNoRepayAmounts").val());
         
         if (borrowTotal > reimbTotal) {
        	 $("#borrowAmount").val(reimbTotal);	 
         } else {
        	 $("#borrowAmount").val(borrowTotal);
         }
         
         if (reimbTotal > borrowTotal) {
        	$("#balance").val(toFix(reimbTotal - borrowTotal));
         } else {
        	$("#balance").val(0); 
         }
	}
}

function toFix(idvalue){
  return parseFloat(idvalue).toFixed(2);//2表示保留2位小數 
}

/**
 * 计算余款
 */
function zjs(){
 var reimbTotal = Number(document.getElementById("reimbTotal").value);
 var borrowAmount = document.getElementById("borrowAmount").value; 
 if (reimbTotal > Number(borrowAmount)) {
	$("#balance").val((reimbTotal- Number(borrowAmount)).toFixed(2));
 } else {
	$("#balance").val("0.00"); 
 }
}

